﻿Imports Entidades
Public Class frmModCliente
    Private cli As New Cliente
    Private lista As New List(Of TipoCliente)
    Private tipo As New TipoCliente
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Me.Close()

    End Sub

    Private Sub frmModCliente_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lista = miGestion.obtenerTiposCliente
        For i As Integer = 0 To lista.Count - 1
            cboTipo.Items.Add(lista(i).nombre)
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If txtNombre.Text = "" Then
            MessageBox.Show("Falta nombre y apellidos")
            Exit Sub
        End If
        cli = miGestion.clientePorNombre(txtNombre.Text)
        If IsNothing(cli) Then
            MessageBox.Show("No se encuentra a este cliente")
            Exit Sub
        End If
        txtEmail.Text = cli.Email
        txtTelefono.Text = cli.Telefono
        For i As Integer = 0 To lista.Count - 1
            If lista(i).id = cli.IdTipoCliente Then
                cboTipo.Text = lista(i).nombre
                tipo = lista(i)
            End If

        Next
    End Sub

    Private Sub cboTipo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboTipo.SelectedIndexChanged
        tipo = lista(cboTipo.SelectedIndex)
        cli.IdTipoCliente = tipo.id
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If txtTelefono.Text = "" Then

        ElseIf Not txtTelefono.Text.Length = 9 OrElse (txtTelefono.Text.StartsWith("9")) AndAlso txtTelefono.Text.StartsWith("7") AndAlso
            txtTelefono.Text.StartsWith("6") Then
            MessageBox.Show("Telefono incorrecto")
            Exit Sub

        End If
        If txtEmail.Text = "" Then

        ElseIf Not txtEmail.Text.Contains("@") OrElse Not txtEmail.Text.Contains(".") Then
            MessageBox.Show("Email incorrecto")
            Exit Sub
        End If
        If txtNombre.Text = "" Then
            MessageBox.Show("Falta el nombre")
            Exit Sub
        End If
        If txtEmail.Text = "" AndAlso txtTelefono.Text = "" Then
            MessageBox.Show("Debe establecer al menos una forma de contacto")
            Exit Sub
        End If
        If cboTipo.Text = "" Then
            MessageBox.Show("Falta el tipo de cliente")
        End If
        cli.Email = txtEmail.Text
        cli.Telefono = txtTelefono.Text
        cli.NombreApellidos = txtNombre.Text
        Dim st As String = miGestion.ModificarCliente(cli)
        If st <> "" Then
            MessageBox.Show(st)
            Exit Sub
        End If
        MessageBox.Show("Cliente modificado correctamente")

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If IsNothing(cli) Then
            MessageBox.Show("No existe este cliente")
            Exit Sub
        End If
        Dim st As String = miGestion.BorrarCliente(cli)
        If st <> "" Then
            MessageBox.Show(st)
        End If
        MessageBox.Show("Cliente borrado correctamente")
        Exit Sub
    End Sub
End Class